/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.query.IsfdModelManagementBoundaryQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementBoundaryVo;
import com.comac.ins.isfd.service.IIsfdModelManagementBoundaryService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 模型管理-边界条件controller
 *
 * @author hxloongs
 * @date 2024-08-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/modelManagementBoundary")
public class IsfdModelManagementBoundaryController extends BaseController {

    @Autowired
    private IIsfdModelManagementBoundaryService isfdModelManagementBoundaryService;

    /**
     * 边界条件分页列表
     */
    @SaCheckPermission("isfd:modelManagementBoundary:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdModelManagementBoundaryVo> list(@RequestBody IsfdModelManagementBoundaryQueryBo bo) {
        return isfdModelManagementBoundaryService.queryPageList(bo);
    }


    /**
     * 获取边界条件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:modelManagementBoundary:query")
    @GetMapping("/{id}")
    public R<IsfdModelManagementBoundaryVo> getInfo(@NotNull(message = "主键不能为空")
                                                    @PathVariable Long id) {
        return R.ok(isfdModelManagementBoundaryService.queryById(id));
    }

    /**
     * 保存边界条件
     */
    @SaCheckPermission("isfd:modelManagementBoundary:save")
    @Log(title = "保存边界条件", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> save(@RequestParam(value = "file", required = false) MultipartFile file,
                        @RequestParam(value = "id", required = false) Long id,
                        @NotEmpty(message = "type保存类型，add代表新增，copy代表复制") @RequestParam(value = "type") String type,
                        @NotEmpty(message = "机型编码不能为空") @RequestParam("modelNumber") String modelNumber,
                        @NotEmpty(message = "备注不能为空") @RequestParam("remarks") String remarks,
                        @NotEmpty(message = "批次号不能为空") @RequestParam("batchNumbers") String batchNumbers) {
        return toAjax(isfdModelManagementBoundaryService.save(file, id, type, modelNumber, remarks, batchNumbers));
    }

    /**
     * 边界条件删除
     */
    @SaCheckPermission("isfd:modelManagementBoundary:remove")
    @Log(title = "边界条件删除", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public R<Void> delete(@RequestBody Long[] ids) {
        return toAjax(isfdModelManagementBoundaryService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 删除预先查询
     */
    @SaCheckPermission("isfd:modelManagementBoundary:pre:delete")
    @PostMapping("/pre/delete")
    public R<Boolean> preDelete(@RequestBody Long[] ids) {
        String resultStr = isfdModelManagementBoundaryService.preDelete(List.of(ids));
        if (StringUtils.isNotBlank(resultStr)) {
            return R.setResult(R.SUCCESS, Boolean.FALSE, resultStr);
        }
        return R.setResult(R.SUCCESS, Boolean.TRUE, null);
    }
}
